package com.webdrider.servlet.action.snippet;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.model.abstracts.AbstractRequest;
import com.common.model.abstracts.AbstractValidator;
import com.common.model.validators.ActionValidator;
import com.common.security.abstracts.AbstractError;
import com.common.security.abstracts.AbstractException;
import com.common.security.errors.ParserError;
import com.common.security.exceptions.ManagerException;
import com.common.security.exceptions.ParserException;
import com.common.servlet.abstracts.AbstractParserServlet;
import com.common.servlet.interfaces.IParser;
import com.webdrider.manager.action.snippet.ArticleManager;
import com.webdrider.manager.interfaces.IArticleManager;
import com.webdrider.model.action.snippet.Article;
import com.webdrider.model.action.snippet.Domain;
import com.webdrider.model.action.snippet.Tag;
import com.webdrider.model.requests.snippet.SearchSnippetArticleRequest;
import com.webdrider.model.response.snippet.SearchSnippetArticleResponse;
import com.webdrider.model.validators.SearchSnippetArticleValidator;
import com.webdrider.model.validators.SessionValidator;
import com.webdrider.security.errors.SnippetError;
import com.webdrider.servlet.util.RequestAction;
import com.webdrider.servlet.util.parser.snippet.SearchSnippetArticleXMLParser;

public class SearchSnippetArticleServlet extends AbstractParserServlet {
	private static final long serialVersionUID = 1L;
	private static final Set<AbstractValidator> validators = new HashSet<AbstractValidator>();

	private SearchSnippetArticleRequest requestContent;
	private SearchSnippetArticleXMLParser parser = new SearchSnippetArticleXMLParser();
	private IArticleManager articleManager = new ArticleManager();
	
	static {
		validators.add(new ActionValidator(RequestAction.SEARCH_SNIPPET_ARTICLES));
		validators.add(new SessionValidator());
		validators.add(new SearchSnippetArticleValidator());
	}
	
	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response) {
		try {
			SearchSnippetArticleResponse searchSnippetArticleResponse = new SearchSnippetArticleResponse();
			
			List<Tag> tags = requestContent.getTags();
			Domain domain = requestContent.getDomain();
			List<Article> articles;
			
			if (domain == null) {
				articles = articleManager.getAllByTags(tags);
			} else {
				articles = articleManager.getAllByTagsAndDomain(tags, domain);
			}
			
			searchSnippetArticleResponse.setRequestAction(requestContent.getAction());
			searchSnippetArticleResponse.setArticles(articles);
			
			response.setContentType("text/xml");
			response.getWriter().write(parser().parseResponse(searchSnippetArticleResponse));
			response.getWriter().close();
		} catch (ManagerException managerException) {
			exception(managerException, response);
		} catch (ParserException parserException) {
			exception(parserException, response);
		} catch (IOException e) {
			error(new ParserError(ParserError.STREAM_ERROR), response);
		}
	}

	@Override
	public void setRequest(AbstractRequest abstractRequest) {
		requestContent = (SearchSnippetArticleRequest) abstractRequest;
	}

	@Override
	public Set<AbstractValidator> getValidators() {
		return validators;
	}

	@Override
	public IParser parser() {
		return parser;
	}
	
	@Override
	public void exception(AbstractException exception, HttpServletResponse response) {
		super.exception(exception, response);
		
		switch (exception.getCode()) {
			 case ManagerException.DATABASE_EXCEPTION: {
				 error(new SnippetError(AbstractError.DATABASE_ERROR), response);
				 break;
			 }
		}
	}
}